home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / Level 1 Extensions 29Sep94 / WrapTextBox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  3.0 KB  |  89 lines  |  [TEXT/KAHL]

  1. /* WrapTextBox.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Audit.h"
  21. #include "Debug.h"
  22. #include "Definitions.h"
  23.  
  24. #include "WrapTextBox.h"
  25. #include "Screen.h"
  26. #include "Memory.h"
  27. #include "DataMunging.h"
  28.  
  29.  
  30. /* draw a string but wordwrap it in the specified box.  The string is null terminated */
  31. void            DrawWrappedTextBox(WinType* Window, char* Text, FontType FontID,
  32.                         FontSizeType FontSize, OrdType X, OrdType Y, OrdType Width, OrdType Height)
  33.     {
  34.         OrdType            FontHeight;
  35.  
  36.         FontHeight = GetFontHeight(FontID,FontSize);
  37.         while (StrLen(Text) > 0)
  38.             {
  39.                 long                PartitionStart;
  40.                 long                PartitionEnd;
  41.  
  42.                 while (Text[0] == ' ')
  43.                     {
  44.                         /* strip leading spaces */
  45.                         Text += 1;
  46.                     }
  47.                 if (StrLen(Text) == 0)
  48.                     {
  49.                         return;
  50.                     }
  51.                 PartitionStart = 1;
  52.                 PartitionEnd = StrLen(Text);
  53.                 /* do a little binary search... */
  54.                 while (PartitionStart < PartitionEnd)
  55.                     {
  56.                         long                MidPoint;
  57.  
  58.                         MidPoint = (PartitionStart + PartitionEnd) / 2;
  59.                         if (LengthOfText(FontID,FontSize,Text,MidPoint,ePlain) > Width)
  60.                             {
  61.                                 /* too long, use short half of interval */
  62.                                 PartitionEnd = MidPoint;
  63.                             }
  64.                          else
  65.                             {
  66.                                 /* too short, we can use the long half of the interval */
  67.                                 if (PartitionStart == MidPoint)
  68.                                     {
  69.                                         /* take care of the rounding-down problem */
  70.                                         PartitionStart = MidPoint + 1;
  71.                                     }
  72.                                  else
  73.                                     {
  74.                                         PartitionStart = MidPoint;
  75.                                     }
  76.                             }
  77.                     }
  78.                 while ((PartitionStart > 1) && (Text[PartitionStart] != ' ')
  79.                     && (Text[PartitionStart] != '-') && (Text[PartitionStart] != 0))
  80.                     {
  81.                         /* make a word-wrap */
  82.                         PartitionStart -= 1;
  83.                     }
  84.                 DrawTextLine(Window,FontID,FontSize,Text,PartitionStart,X,Y,ePlain);
  85.                 Y += FontHeight;
  86.                 Text += PartitionStart;
  87.             }
  88.     }
  89.